package jacidco;

/**
 * Marco para la creacion y registro de Materiales
 * @author David Cuellar
 */
public class CreaMaterialFrame extends javax.swing.JFrame implements BotonesNuevoCrearCancelar
{

    /** Creates new form CrearMaterial */
    public CreaMaterialFrame() 
    {
        initComponents();
        
        getNextCode();
        txtNombreNivel.requestFocus();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lblTitulo4 = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        lblTitulo = new javax.swing.JLabel();
        btmCrear = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        lblCodNivel = new javax.swing.JLabel();
        txtCodMaterial = new javax.swing.JTextField();
        lblNombreMaterial = new javax.swing.JLabel();
        txtNombreNivel = new javax.swing.JTextField();
        lblDescripcionNivel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaDescripcionMaterial = new javax.swing.JTextArea();
        lblMarcareg = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setBackground(new java.awt.Color(255, 51, 51));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jacidco/resources/Logo_Jacidco_mini.png"))); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        lblTitulo4.setFont(new java.awt.Font("Times New Roman", 3, 36));
        lblTitulo4.setText("Material de Estudio");
        lblTitulo4.setName("lblTitulo4"); // NOI18N

        btnCancelar.setFont(new java.awt.Font("Tahoma", 3, 11));
        btnCancelar.setText("Cancelar");
        btnCancelar.setName("btnCancelar"); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnNuevo.setFont(new java.awt.Font("Tahoma", 3, 11));
        btnNuevo.setText("Nuevo");
        btnNuevo.setName("btnNuevo"); // NOI18N
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        lblTitulo.setFont(new java.awt.Font("Times New Roman", 3, 18));
        lblTitulo.setText("Comunidad Dinamica en Java");
        lblTitulo.setName("lblTitulo"); // NOI18N

        btmCrear.setFont(new java.awt.Font("Tahoma", 3, 11));
        btmCrear.setText("Crear");
        btmCrear.setName("btmCrear"); // NOI18N
        btmCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btmCrearActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setName("jPanel1"); // NOI18N

        lblCodNivel.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblCodNivel.setText("Codigo Material :");
        lblCodNivel.setName("lblCodNivel"); // NOI18N

        txtCodMaterial.setEditable(false);
        txtCodMaterial.setName("txtCodMaterial"); // NOI18N

        lblNombreMaterial.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblNombreMaterial.setText("Nombre :");
        lblNombreMaterial.setName("lblNombreMaterial"); // NOI18N

        txtNombreNivel.setName("txtNombreNivel"); // NOI18N

        lblDescripcionNivel.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblDescripcionNivel.setText("Descripcion :");
        lblDescripcionNivel.setName("lblDescripcionNivel"); // NOI18N

        jScrollPane1.setToolTipText("");
        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txaDescripcionMaterial.setColumns(20);
        txaDescripcionMaterial.setFont(new java.awt.Font("Tahoma", 0, 11));
        txaDescripcionMaterial.setLineWrap(true);
        txaDescripcionMaterial.setRows(5);
        txaDescripcionMaterial.setName("txaDescripcionMaterial"); // NOI18N
        jScrollPane1.setViewportView(txaDescripcionMaterial);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblCodNivel)
                    .addComponent(lblNombreMaterial)
                    .addComponent(lblDescripcionNivel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtCodMaterial, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                    .addComponent(txtNombreNivel))
                .addGap(96, 96, 96))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodNivel)
                    .addComponent(txtCodMaterial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombreMaterial)
                    .addComponent(txtNombreNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDescripcionNivel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        lblMarcareg.setFont(new java.awt.Font("Times New Roman", 0, 11));
        lblMarcareg.setText("Copyright 2011. DCM. All Rights Reserved");
        lblMarcareg.setName("lblMarcareg"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 456, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblTitulo, 0, 0, Short.MAX_VALUE)
                            .addComponent(lblTitulo4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnNuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btmCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblMarcareg))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 505, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTitulo)
                        .addGap(67, 67, 67)
                        .addComponent(lblTitulo4)))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btmCrear)
                    .addComponent(btnNuevo))
                .addGap(18, 18, 18)
                .addComponent(lblMarcareg)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        
        Main.goToMenuPrincipal();
        dispose();
        
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        
        limpiarCampos();
        getNextCode();

    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btmCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btmCrearActionPerformed
        
        capturaDatos();
        guardaDatos();

    }//GEN-LAST:event_btmCrearActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btmCrear;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCodNivel;
    private javax.swing.JLabel lblDescripcionNivel;
    private javax.swing.JLabel lblMarcareg;
    private javax.swing.JLabel lblNombreMaterial;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JLabel lblTitulo4;
    private javax.swing.JTextArea txaDescripcionMaterial;
    private javax.swing.JTextField txtCodMaterial;
    private javax.swing.JTextField txtNombreNivel;
    // End of variables declaration//GEN-END:variables

    /**
     * Limpia y habilita los campos para ingresar un nuevo Material
     */
    public boolean limpiarCampos()
    {
        txtNombreNivel.setText("");
        txaDescripcionMaterial.setText("");
        txtNombreNivel.setEnabled(true);
        txtNombreNivel.requestFocus();
        txaDescripcionMaterial.setEnabled(true); 
        return true;
    }
    
    /**
     * Asigna los datos de los JTextField a los atributos del Objeto Material. Codigo, Nombre y Descripcion
     * @see Material
     */
    public boolean capturaDatos()
    {
        Main.material.setCod(Byte.parseByte(txtCodMaterial.getText()));
        Main.material.setNombre(txtNombreNivel.getText());
        Main.material.setDescripcion(txaDescripcionMaterial.getText());
        return true;
    }
    
    /**
     * Realiza la validacion de campos vacios y guarda la informacion de la Base de Datos
     * @see BaseDatos
     */
    public boolean guardaDatos()
    {
        if(!Main.material.getNombre().equals("") && !Main.material.getDescripcion().equals(""))
        {
            Main.bd.conectar();
            if(Main.bd.guardaDatosMaterial(Main.material.getCod(), Main.material.getNombre(), Main.material.getDescripcion()))
            {
                Main.mensaje.datosGuardaosOK("Creacion de Material");
                txtNombreNivel.setEnabled(false);
                txaDescripcionMaterial.setEnabled(false);
            }
        }
        else
        {
            Main.mensaje.noCamposVacios();
        }
        return true;
    }
    
    public void getNextCode()
    {
        Main.bd.conectar();
        txtCodMaterial.setText(""+(Main.bd.nextCode("cod_material", "material")+1));
        Main.bd.cerrarConexion();
    }
    
}
